package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.UserStaff;
import com.ruoyi.system.service.IUserStaffService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 管理员Controller
 * 
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/system/staff")
public class UserStaffController extends BaseController
{
    private String prefix = "system/staff";

    @Autowired
    private IUserStaffService userStaffService;

    @RequiresPermissions("system:staff:view")
    @GetMapping()
    public String staff()
    {
        return prefix + "/staff";
    }

    /**
     * 查询管理员列表
     */
    @RequiresPermissions("system:staff:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserStaff userStaff)
    {
        startPage();
        List<UserStaff> list = userStaffService.selectUserStaffList(userStaff);
        return getDataTable(list);
    }



    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserStaff userStaff)
    {
        List<UserStaff> list = userStaffService.selectUserStaffList(userStaff);
        ExcelUtil<UserStaff> util = new ExcelUtil<UserStaff>(UserStaff.class);
        return util.exportExcel(list, "staff");
    }


    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存管理员
     */
    @RequiresPermissions("system:staff:add")
    @Log(title = "管理员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserStaff userStaff)
    {
        return toAjax(userStaffService.insertUserStaff(userStaff));
    }

    /**
     * 修改管理员
     */
    @GetMapping("/edit/{staffId}")
    public String edit(@PathVariable("staffId") Long staffId, ModelMap mmap)
    {
        UserStaff userStaff = userStaffService.selectUserStaffById(staffId);
        mmap.put("userStaff", userStaff);
        return prefix + "/edit";
    }

    /**
     * 修改保存管理员
     */
    @RequiresPermissions("system:staff:edit")
    @Log(title = "管理员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserStaff userStaff)
    {
        return toAjax(userStaffService.updateUserStaff(userStaff));
    }

    /**
     * 删除管理员
     */
    @RequiresPermissions("system:staff:remove")
    @Log(title = "管理员", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userStaffService.deleteUserStaffByIds(ids));
    }
}
